package com.sdp.irrigation.controller;


import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.irrigation.cache.IrrigationCleanCache;
import com.sdp.irrigation.db.irrigation_field.service.IIrrigationFieldService;
import com.sdp.irrigation.db.irrigation_field.service.impl.IrrigationFieldServiceImpl;
import com.sdp.irrigation.entity.IrrigationField;
import com.sdp.irrigation.view.IrrigationFieldView;

/**
 * <p>
 * 田块台账信息 前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-12-14
 */
@RestController
@RequestMapping("/IrrigationFieldController")
public class IrrigationFieldController {

	@Autowired
	private IIrrigationFieldService iIrrigationFieldService;
	
	@Autowired
	private Query query;
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;	
	
	@Autowired
	private IrrigationCleanCache irrigationCleanCache;
	
	@Autowired
	private IrrigationFieldServiceImpl irrigationFieldServiceImpl;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iIrrigationFieldService, IrrigationField.class, dataCenter));
	}
	
	@PostMapping(value = "/viewPage")
	public Result<?> viewPage(@RequestBody DataCenter dataCenter) {
		QueryWrapper<IrrigationFieldView> queryWrapper = new QueryWrapper<>();
		return Result.ok(query.viewPage(irrigationFieldServiceImpl, IrrigationFieldView.class, dataCenter, queryWrapper));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody IrrigationField field) {
		if(field==null) {
			return Result.error("田块信息为空");
		}
		if(StringUtils.isEmpty(field.getKeyId())) {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iIrrigationFieldService, IrrigationField.class, new String[] {"fieldNo","fieldName"}, "keyId", field, null, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("田块名称和编号不能重复");
			}
			field.setCreateTime(LocalDateTime.now());
			field.setUpdateTime(LocalDateTime.now());
			iIrrigationFieldService.save(field);
		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iIrrigationFieldService, IrrigationField.class, new String[] {"fieldNo","fieldName"}, "keyId", null, field, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("田块名称和编号不能重复");
			}
			field.setUpdateTime(LocalDateTime.now());
			iIrrigationFieldService.updateById(field);
		}
		
		irrigationCleanCache.cleanFieldCache();
		return Result.ok();
	}
}

